package com.tianhao.shell.controller;

import com.tianhao.shell.constant.Constants;
import com.tianhao.shell.domain.ModelDomain;
import com.tianhao.shell.service.ShellService;
import com.tianhao.shell.util.ZipUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;

/**
 * @ClassName: TestController
 * @Description: 服务启动及关闭脚本生成
 * @author: tianhao.luo@hand-china.com
 * @date: 2020/11/4  11:21
 */
@RestController
@RequestMapping("/sh")
public class DownloadController {

    @Autowired
    private ShellService shellService;

    @GetMapping("/startup")
    public void downloadStart(ModelDomain domain, HttpServletResponse response) throws IOException {
        String fileName = "service-start-sh.zip";
        shellService.createStartupShell(domain, response);

        File fileSource = new File(Constants.BASE_OUTPUT_PATH);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename="+fileName);
        response.setContentType("application/octet-stream; charset=UTF-8");
        // 将文件保留目录结构打包成zip输出到前端
        ZipUtils.toZip(fileSource.getPath(), response.getOutputStream(), true);
        // 删除文件
        Files.walk(Paths.get(fileSource.getPath())).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }

    @GetMapping("/shutdown")
    public void downloadShut(ModelDomain domain, HttpServletResponse response) throws IOException {
        String fileName = "service-shutdown-sh.zip";
        shellService.createShutDownShell(domain, response);

        File fileSource = new File(Constants.BASE_OUTPUT_PATH);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename="+fileName);
        response.setContentType("application/octet-stream; charset=UTF-8");
        // 将文件保留目录结构打包成zip输出到前端
        ZipUtils.toZip(fileSource.getPath(), response.getOutputStream(), true);
        // 删除文件
        Files.walk(Paths.get(fileSource.getPath())).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }

    @GetMapping("/startup/all")
    public void downloadStartAll(ModelDomain domain, HttpServletResponse response) throws IOException {
        String fileName = "all-service-start-sh.zip";
        //去除非法空格
        domain.trim();
        shellService.createStartupShell(domain, response);
        shellService.createStartupShellAll(domain, response);
        shellService.createMoveStartupShellAll(domain,response);
        shellService.createReadMe();
        //创建关闭脚本

        File fileSource = new File(Constants.BASE_OUTPUT_PATH);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename="+fileName);
        response.setContentType("application/octet-stream; charset=UTF-8");
        // 将文件保留目录结构打包成zip输出到前端
        ZipUtils.toZip(fileSource.getPath(), response.getOutputStream(), true);
        // 删除文件
        Files.walk(Paths.get(fileSource.getPath())).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }

    @GetMapping("/shutdown/all")
    public void downloadShutdownAll(ModelDomain domain, HttpServletResponse response) throws IOException {
        String fileName = "all-service-shutdown-sh.zip";
        //去除非法空格
        domain.trim();
        shellService.createShutDownShell(domain, response);
        shellService.createShutDownShellAll(domain, response);
        shellService.createMoveShutdownShellAll(domain,response);
        shellService.createReadMe();
        //创建关闭脚本

        File fileSource = new File(Constants.BASE_OUTPUT_PATH);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename="+fileName);
        response.setContentType("application/octet-stream; charset=UTF-8");
        // 将文件保留目录结构打包成zip输出到前端
        ZipUtils.toZip(fileSource.getPath(), response.getOutputStream(), true);
        // 删除文件
        Files.walk(Paths.get(fileSource.getPath())).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }
}
